<template>
	<view class="pos-r">
		<view class="flex-r flex-start pos-r flex-fill" v-if="isbool(itemEntity)">
			<view class="">
				<image @tap="viewPic(qr(itemEntity.code),[qr(itemEntity.code)])" class="wh25-25" mode="aspectFill" :src="qr(itemEntity.code)"></image>
			</view>
			<view class="flex1 padl3 fs8 lh2 c12">
				<view class="fwb fs12 c11 flrc">
					<view class="flex1">{{itemEntity.code}}</view>
					<view class="cu-btn sm line-blue round marl2" @tap="copy(itemEntity.code)">复制</view>
				</view>
				<view><text>仓库：</text>{{isnull(itemEntity.store)?'未设置':itemEntity.store.name}}</view>
				<view><text>创建时间：</text>{{datetime(itemEntity.createAt)}}</view>
				<view><text>审核时间：</text>{{datetime(itemEntity.checkAt)}}</view>
			</view>
		</view>
		<view class="list-style-2 mart3 marb2 flrc">
			<text class="flex1">申请物资</text>
			<text class="line-blue cu-btn sm round">{{isnull(itemEntity.type)?'未设置':itemEntity.type.name}}</text>
			</view>
		<view class="pad2 bc13">
			<view class="mart2 bdbda padb2" v-for="item,index in itemEntity.items">
				<store-item :item="item"></store-item>
				<view class="flrc tar pad2 bcf fwb" v-if="isbool(itemEntity.nums) && itemEntity.nums.length === itemEntity.items.length">
					<view>数量：</view>
					<view class="flex1">{{itemEntity.nums[index]}}</view>
				</view>
			</view>
		</view>
		<view class="pad10" v-if="!isbool(itemEntity)">
			<view class="pad10 bdda tac c12">无效的数据</view>
		</view>
		<view class="pos-a l0 r0 t0 b0 flcc bcaf4 c12" v-show="loading && !isbool(itemEntity)">
			<text class="fa fa-spinner fa-spin fa-4x"></text>
		</view>
	</view>
</template>

<script>
	import storeItem from './store_item.vue'
	export default {
		components:{
			storeItem
		},
		props: {
			value: '',
			item: '',
			itemId: '',
			iri: '',
			code: ''
		},
		data() {
			return {
				itemEntity: '',
				ready: false,
				loading: false,
			}
		},
		methods: {
			getById(val) {
				this.loading = true
				this.$api.get("/api/storeItems/" + val, {}, res => {
					this.loading = false
					this.log(res)
					this.itemEntity = res.d
				})
			},
			getByCode(code) {
				this.loading = true
				this.$api.get("/api/storeItems", {
					_where: {
						eq: {
							code
						}
					}
				}, res => {
					this.loading = false
					this.log(res)
					if (res.d.length == 1) {
						this.itemEntity = res.d[0]
					} else {
						if (res.d.length == 0) {
							this.$com.showToast('未找到物资')
						} else {
							this.$com.showToast('检测到重复的编号，请管理后台核对')
						}
					}
				})
			},
			update() {
				if (this.isbool(this.item)) {
					this.log(this.item)
					this.itemEntity = this.item
					return
				}
				if (this.isbool(this.id)) {
					this.getById(this.id)
					return
				}
				if (this.isbool(this.iri)) {
					this.getById(this.iri.split("/").pop())
					return
				}
				if (this.isbool(this.code)) {
					this.getByCode(this.code)
					return
				}
			}
		},
		created() {
			this.log("created")
		},
		mounted() {
			this.log("mounted")
			// #ifdef MP-WEIXIN || APP-PLUS
			if(this.isbool(this.value)) this.getById((this.value + '').split("/").pop())
			// #endif
			this.update()
		},
		watch: {
			id(val) {
				this.log("watch id")
				if (!this.$com.isbool(val)) return
				this.getById(val)
			},
			code(val) {
				this.log("watch code")
				if (!this.$com.isbool(val)) return
				this.getByCode(val)
			},
			value(val) {
				this.log("watch value")
				if (!this.$com.isbool(val)) return
				this.getById(val.split("/").pop())
			},
			itemEntity(item) {
				this.log("watch itemEntity")
				if (!this.$com.isbool(item)) return
				this.$emit('input', '/api_/store_items/' + item.id)
			}
		}
	}
</script>

<style>

</style>
